#include <tchar.h>
#include "CdiEngine.h"
#include "CdiRenderBox.h"

// Globals

static CdiEngine* g_diWindow = NULL;
void Setup();
void SetupObjects();
void SetupLights();

// Window Handler
LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) 
{
  switch( Msg )
  {
    case WM_QUIT:
    case WM_DESTROY:
    case WM_CLOSE:
      {
        PostQuitMessage( 0 );
        return 0;
      }

    case WM_KEYDOWN:
      {
        switch( wParam ) 
        {
        case VK_ESCAPE:
          {
            PostQuitMessage( 0 );
          } return TRUE;
        }
      }
  }

  if( g_diWindow != NULL ) 
  {
    g_diWindow->HandleInput( Msg, wParam, lParam );
  }

  return (LRESULT) DefWindowProc( hWnd, Msg, wParam, lParam );
}

// Main
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmd, int nShowCmd )
{
  CdiEngine* pWindow = NULL;  
  pWindow = new CdiEngine( WndProc, false, 800,600 );
  g_diWindow = pWindow;
  pWindow->SetTitle( _T("Gears") );
  
  Setup();

  if( true == pWindow->IsInit() )
  {
    pWindow->Start();
  }
  
  if( pWindow != NULL ) 
  {
    delete pWindow;
  }  
  
  pWindow = NULL;
  g_diWindow = NULL;
}

void Setup()
{
  if( g_diWindow != NULL )
  {
    SetupObjects();
    SetupLights();
    g_diWindow->SetLightingState( FALSE );
    if( g_diWindow->GetCamera() != NULL )
    {
      g_diWindow->GetCamera()->SetScale( 0.10f );
    }
  }
}

void SetupObjects()
{
  g_diWindow->AddObject( new CdiRenderBox( g_diWindow->GetDevice(), 0.5f ) ); 
}

void SetupLights()
{
  g_diWindow->AddLight( new CdiLightingBase( g_diWindow->GetDevice() ) );
}